/**
 * 
 */
package com.oolong.account.web;

import java.math.BigDecimal;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.oolong.account.dal.InitemDAO;
import com.oolong.account.dal.data.Initem;
import com.oolong.account.model.InItemModel;
import com.oolong.account.model.InTypeModel;
import com.oolong.account.model.UserModel;
import com.oolong.account.model.factory.TypeFactory;
import com.oolong.account.service.InManage;
import com.oolong.account.util.ParamterCheck;

/**
 * @author oolong
 * 
 */
// 标记它是个SPRING的BEAN，是MVC里的C
@Controller
// 表示这个类就是reqOutItem.do
@RequestMapping("/editInItem.do")
public class InItemEditController {
	Logger logger = Logger.getLogger(InItemEditController.class);
	// 表示需要SPRING自动注入
	@Autowired
	private InManage inManage;
	@Autowired
	private TypeFactory typeFactory;
	@Autowired
	private LoadGlobalParameter loadGlobalParameter;
	@Autowired
	InitemDAO initemDAO;
	String message = " ";

	@RequestMapping(method = RequestMethod.GET)
	public String processInItem(@RequestParam("inid") int inid, ModelMap model,
			HttpServletResponse response, HttpSession session) {
		message = " ";
		InItemModel inItemModel;
		response.setContentType("text/html;charset=UTF-8");
		UserModel userModel = (UserModel) session.getAttribute("user");
		Initem initem = initemDAO.selectByPrimaryKey(inid);
		if (initem != null) {
			if (userModel.getId() != initem.getUserId()) {
				message = "你无权访问该记录：" + inid;
				model.addAttribute("message", message);
				return "deleteOutItem";
			} else {
				inItemModel = inManage.querybyID(inid);
				model.addAttribute("initem", inItemModel);
				model.addAttribute("intypes", loadGlobalParameter.globalinType);
			}
		} else {
			message = "你无权访问该记录:" + inid;
			model.addAttribute("message", message);
			return "deleteOutItem";
		}
		return "editInItem";
	}

	@RequestMapping(method = RequestMethod.POST)
	public String processInItem(@RequestParam("inid") int inid,
			@RequestParam("money") String money,
			@RequestParam("intype") String intype,
			@RequestParam("memo") String memo, HttpServletResponse response,
			HttpSession session, ModelMap model) {
		UserModel userModel = (UserModel) session.getAttribute("user");
		Initem initem = initemDAO.selectByPrimaryKey(inid);
		message = " ";
		response.setContentType("text/html;charset=UTF-8");
		if (initem != null) {
			if (userModel.getId() != initem.getUserId()) {
				message = "你无权修改该记录：" + inid;
				model.addAttribute("message", message);
				return "deleteOutItem";
			} else {
				InItemModel inItemModel = new InItemModel();
				inItemModel.setId(inid);
				// 设置金额
				BigDecimal bigmoney = null;
				try {
					bigmoney = new BigDecimal(money);
					inItemModel.setMoney(bigmoney);
				} catch (NumberFormatException e) {
					message = "传入的金额不正确";
					model.addAttribute("message", message);
					return "deleteOutItem";
				}
				// 设置备注
				if (ParamterCheck.checkMemo(memo)) {
					inItemModel.setMemo(memo);
				} else {
					message = "传入的备注信息不正确，不得大于128个字符";
					model.addAttribute("message", message);
					return "deleteOutItem";
				}
				// 设置收入类型
				int intypeid = Integer.valueOf(intype).intValue();
				if (!ParamterCheck.oneTypeCheck(intypeid)) {
					message = "传入的收入类型不正确，请不要篡改为不存在的收入类型";
					model.addAttribute("message", message);
					return "deleteOutItem";
				}
				InTypeModel inTypeModel = typeFactory.getInTypeModel(intypeid);
				inItemModel.setUser(userModel);
				inItemModel.setInType(inTypeModel);
				boolean updateresult = inManage.update(inItemModel);
				inItemModel = inManage.querybyID(inid);
				model.addAttribute("initem", inItemModel);
				model.addAttribute("intypes", loadGlobalParameter.globalinType);
				if (updateresult) {
					message = "修改成功，ID为：" + inid;
					model.addAttribute("message", message);
				} else {
					message = "修改失败，请联系管理员";
					model.addAttribute("message", message);
					return "deleteInItem";
				}
			}
		} else {
			message = "你无权修改该记录:" + inid;
			model.addAttribute("message", message);
			return "deleteOutItem";
		}
		return "editInItem";
	}
}
